<template>
  <div class="image-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>素材管理</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- /面包屑导航 -->
      </div>
      <!-- 素材内容 -->
      <div class="content">
        <div class="radio-groups">
          <el-radio-group
            v-model="collect"
            size="mini"
            @change="onCollectChange"
          >
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>
          </el-radio-group>
          <el-button
            size="mini"
            type="success"
            @click="dialogUploadVisible = true"
          >上传素材</el-button>
        </div>
        <el-row :gutter="10">
          <el-col
            :xs="12"
            :sm="6"
            :md="6"
            :lg="4"
            v-for="(img, index) in images"
            :key="index"
          >
            <el-image
              style="height: 100px"
              :src="img.url"
              fit="cover">
                <!-- <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div> -->
              </el-image>
          </el-col>
        </el-row>
      </div>
      <!-- /素材内容 -->
    </el-card>
    <el-dialog
      class="dialog"
      title="上传素材"
      :append-to-body="true"
      :visible.sync="dialogUploadVisible"
    >
      <!--
        upload 组件本身就支持请求提交上传操作，说白了你使用它不用自己去发送请求，它会自动发送。
        它内部的倾情方法：默认就是 POST
        请求路径： action 必须写完整路径，这里的路径根axios的路径不同，axios路径中有baseURL，这里必须是完整路径
        请求头： headers
       -->
      <el-upload
        v-if="dialogUploadVisible"
        class="upload-demo"
        drag
        action="http://api-toutiao-web.itheima.net/mp/v1_0/user/images"
        :headers="uploadHeaders"
        name="image"
        :on-success="onUploadSuccess"
        multiple
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import { getImages } from '@/api/image'
export default {
  name: 'ImageIndex',
  components: {},
  props: {},
  data () {
    const user = JSON.parse(window.localStorage.getItem('admin-user'))
    return {
      collect: false, // 默认查询全部素材列表
      images: [], // 存放素材图片
      dialogUploadVisible: false, // 默认不显示遮罩层，当点击上传素材是才让其显示
      uploadHeaders: {
        Authorization: `Bearer ${user.token}`
      }
    }
  },
  computed: {},
  watch: {},
  created () {
    this.loadImages(false)
  },
  mounted () {},
  methods: {
    loadImages (collect = false) {
      getImages({
        collect
      }).then(res => {
        this.images = res.data.data.results
      })
    },

    onCollectChange (value) {
      this.loadImages(value)
    },

    onUploadSuccess () {
      // 提示框
      this.$message({
        type: 'success',
        message: '上传成功',
        center: true
      })
      // 图片上传成功后，关闭上传窗口
      this.dialogUploadVisible = false
      // 重新渲染页面中的图片数据
      this.loadImages(false)
    }
  }
}
</script>

<style scoped lang="less">
  .content {
    .radio-groups {
      display: flex;
      justify-content: space-between;
      margin-bottom: 20px;
    }
  }
  .dialog {
    /deep/ .el-dialog {
      width: 29.2%;
    }
  }
</style>
